package io.lckgroup.modules.lck.controller.wxgzh;

import io.lckgroup.common.utils.PageUtils;
import io.lckgroup.common.utils.R;
import io.lckgroup.modules.lck.annotation.WxLogin;
import io.lckgroup.modules.lck.annotation.WxLoginUser;
import io.lckgroup.modules.lck.entity.CustomerInfoEntity;
import io.lckgroup.modules.lck.entity.OrderDetailEntity;
import io.lckgroup.modules.lck.entity.OrderMainEntity;
import io.lckgroup.modules.lck.form.wxgzh.OrderEntityWxgzhForm;
import io.lckgroup.modules.lck.service.OrderService;
import io.lckgroup.modules.lck.utils.MakeOrderIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 公众号订单controller
 */
@RestController
@RequestMapping("/mp/wxgzhOrder")
public class WxgzhOrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 生成订单
     *
     * @param orderEntityWxgzhForm
     * @param customerInfoEntity
     * @return
     */
    @PostMapping("/createOrder")
    @WxLogin
    public R createOrder(@RequestBody OrderEntityWxgzhForm orderEntityWxgzhForm, @WxLoginUser CustomerInfoEntity customerInfoEntity) {
        //判断商家是否打烊
        boolean isOpen = orderService.isMerchinfoOpen(orderEntityWxgzhForm.getMerchantId());
        if (!isOpen) {
            return R.error("不好意思，商家已经打烊了");
        }
        //计算订单总价
        BigDecimal orderPrice = orderService.countOrderPrice(orderEntityWxgzhForm.getFoodList());
        //生成订单号
        String orderNum = MakeOrderIdUtil.makeOrderId();
        OrderMainEntity orderMainEntity = new OrderMainEntity();
        //插入订单表
        OrderMainEntity orderMainEntity1 = orderService.createOrder(orderMainEntity, orderPrice, orderNum, orderEntityWxgzhForm, customerInfoEntity);
        if (orderMainEntity1 == null) {
            return R.error("菜品信息已更新，请重新下单");
        } else {
            return R.ok().put("orderNum", orderMainEntity1.getOrderId()).put("merchantId", orderMainEntity1.getMerchantId());
        }
    }

    /**
     * 查询订单明细
     *
     * @param id 主订单id
     * @return
     */
    @GetMapping("/getOrderDetail/{id}")
    @WxLogin
    @Cacheable(value = "orderDetail", key = "#id")
    public R getOrderDetail(@PathVariable Long id) {
        List<OrderDetailEntity> orderDetailList = orderService.getOrderDetail(id);
        return R.ok().put("list", orderDetailList);
    }

    /**
     * 根据用户id查询订单（按订单状态分页查询）
     *
     * @param params（里面包含分页参数及订单状态参数）
     * @param customerInfoEntity
     * @return
     */
    @GetMapping("/getOrderByState")
    @WxLogin
    public R getOrderByState(@RequestParam Map<String, Object> params, @WxLoginUser CustomerInfoEntity customerInfoEntity) {
        PageUtils list = orderService.getOrderByState(params, customerInfoEntity);
        return R.ok().put("order", list);
    }

    /**
     * 取消未支付的订单
     *
     * @param orderNum
     * @return
     */
    @RequestMapping("/cancelOrder/{orderNum}")
    @WxLogin
    public R cancelOrder(@PathVariable String orderNum) {
        if ("" != orderNum) {
            int result = orderService.cancelOrder(orderNum);
            if (1 == result) {
                return R.ok("取消订单成功");
            } else {
                return R.error("取消订单失败");
            }
        }
        return R.error("订单号为空");
    }
}
